package com.dhcc.bpm.modules.safe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.safe.entity.GatewayControl;
import com.dhcc.bpm.modules.safe.entity.IpGateway;
import com.dhcc.bpm.modules.safe.service.IGatewayControlService;
import com.dhcc.bpm.modules.safe.service.IIpGatewayService;
import com.dhcc.bpm.modules.safe.vo.IpGatewayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

 /**
 * @ClassName IpGatewayController
 * @Description 网关访问控制IP关联表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16* @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-safe/ipgateway")
@Api(value = "网关访问控制IP关联表", tags = "网关访问控制IP关联表接口")
public class IpGatewayController extends BladeController {

	 private IIpGatewayService ipGatewayService;

	 private IGatewayControlService gatewayControlService;

	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:02 2020/2/22
	 * @param ipGateway
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.safe.entity.IpGateway>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入ipGateway")
	@ApiLog("详情")
	@Log(title = "详情",businessType = BusinessType.SELECT)
	public R<IpGateway> detail(IpGateway ipGateway) {
		IpGateway detail = ipGatewayService.getOne(Condition.getQueryWrapper(ipGateway));
		return R.data(detail);
	}

	/**
	 * @Description 分页 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:02 2020/2/22
	 * @param ipGateway
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.entity.IpGateway>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入ipGateway")
	@ApiLog("分页")
	@Log(title = "分页",businessType = BusinessType.SELECT)
	public R<IPage<IpGateway>> list(IpGateway ipGateway, Query query) {
		IPage<IpGateway> pages = ipGatewayService.page(Condition.getPage(query), Condition.getQueryWrapper(ipGateway));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:03 2020/2/22
	 * @param ipGateway
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.vo.IpGatewayVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入ipGateway")
	@ApiLog("自定义分页 网关访问控制IP关联表")
	@Log(title = "自定义分页 网关访问控制IP关联表",businessType = BusinessType.SELECT)
	public R<IPage<IpGatewayVO>> page(IpGatewayVO ipGateway, Query query) {
		IPage<IpGatewayVO> pages = ipGatewayService.selectIpGatewayPage(Condition.getPage(query), ipGateway);
		return R.data(pages);
	}

	/**
	 * @Description 新增 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:03 2020/2/22
	 * @param ipGateway
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入ipGateway")
	@ApiLog("新增")
	@Log(title = "新增",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody IpGateway ipGateway) {
		return R.status(ipGatewayService.save(ipGateway));
	}

	/**
	 * @Description 修改 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:03 2020/2/22
	 * @param ipGateway
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入ipGateway")
	@ApiLog("修改")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody IpGateway ipGateway) {
		return R.status(ipGatewayService.updateById(ipGateway));
	}

	/**
	 * @Description 新增或修改 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:03 2020/2/22
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入ipGateway")
	@ApiLog("新增或修改")
	@Log(title = "新增或修改",businessType = BusinessType.UPDATE)
	public R submit(@RequestBody JSONObject obj) {

		boolean status = true;
		IpGateway ipGateway = new IpGateway();
		JSONObject.JSONArray list = obj.getArr("ipAddress");
		ipGateway.setOpcControlId(Long.parseLong(obj.getStr("opcControlId")));
		for (int i = 0; i < list.size(); i++) {
			ipGateway.setIpAddress((String) list.get(i));
			try {
				status =ipGatewayService.saveOrUpdate(ipGateway);
			} catch (Exception e) {
				if (e.getMessage().contains("for key 'gate_address_idx'") || e.getMessage().contains("ORA-00001")) {
					return R.fail(500, list.get(i) + "，该Ip地址已存在");
				}
				return R.fail(500, "数据库异常");
			}
			ipGateway.setId(null);
			// 修改刷新状态
			GatewayControl control = new  GatewayControl();
			control.setId(Long.parseLong(obj.getStr("opcControlId")));
			control.setRefreshFlag("1");
			gatewayControlService.updateById(control);
			if(status == false){
				return R.status(status);
			}
		}

		return R.status(status);
	}


	/**
	 * @Description 删除 网关访问控制IP关联表
	 * @Author daina
	 * @Date 10:03 2020/2/22
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除")
	@Log(title = "删除",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(ipGatewayService.deleteLogic(Func.toLongList(ids)));
	}


}
